package com.fwzc.giuseppe.util;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 通用的ViewHolder
 * @author llf
 *
 */
public class ViewHolder {
	private SparseArray<View> mViews;  
    private final View mConvertView;  
  
    private ViewHolder(Context context, ViewGroup parent, int layoutId)  
    {  
        this.mViews = new SparseArray<View>();  
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,  
                false); 
        mConvertView.setTag(this);   
    }  
 
    public static ViewHolder get(Context context, View convertView, 
            ViewGroup parent, int layoutId)  
    {  
      
        if (convertView == null)  
        {  
            return new ViewHolder(context, parent, layoutId);  
        }  
        return (ViewHolder) convertView.getTag();  
    }  

    public ViewHolder setText(int viewId, String text)  
    {  
        TextView view = (TextView)getView(viewId);  
        view.setText(text);  
        return this;  
    }  
  
    public ViewHolder setImageResource(int viewId, int drawableId)  
    {  
        ImageView view = (ImageView)getView(viewId);  
        view.setImageResource(drawableId);  
  
        return this;  
    }  
 
    public View getView(int viewId)  
    {  
          
        View view = mViews.get(viewId);  
        if (view == null)  
        {  
            view = mConvertView.findViewById(viewId);  
            mViews.put(viewId, view);  
        }  
        return view;  
    }  
  
    public View getConvertView()  
    {  
        return mConvertView;  
    }  
}
